import os.path as op
from loguru import logger as log
from check_log_grab.common import read_json, save_json


class ReptileConfig(object):

    def __init__(self, fp: str):
        if op.exists(fp) is False:
            log.error("配置文件异常，请重新在模板中配置")
            self.__generate__(fp)
            exit()
        self.config = read_json(fp)

    def __generate__(self, fp):
        cf = {"ZK": [{"ip": "", "port": 4370, "title": "", "detail": ""}],
              "WX": {
                  "corp_id": "",
                  "corp_secret": "",
                  "redis_ip": ""
              }
              }
        save_json(fp, cf)

    def __Assemble_ZK__(self):
        self.config['ZK_FLAG'] = False
        conf = self.config.get("ZK", [])
        if len(conf) > 0:
            for zc in conf:
                if zc.get("ip", "") in "":
                    return
            self.config['ZK_FLAG'] = True

    def __Assemble_WX__(self):
        self.config['WX_FLAG'] = False
        conf = self.config.get("WX", {})
        if conf.get("corp_id", "") in "":
            return
        if conf.get("corp_secret", "") in "":
            return
        self.config['WX_FLAG'] = True

    def loads(self) -> dict:
        self.__Assemble_ZK__()
        self.__Assemble_WX__()
        return self.config
